home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / ov-re-mat.cc < prev    next >
C/C++ Source or Header  |  1996-11-07  |  5KB  |  274 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "lo-ieee.h"
  32. #include "lo-utils.h"
  33. #include "mx-base.h"
  34.  
  35. #include "gripes.h"
  36. #include "mappers.h"
  37. #include "oct-obj.h"
  38. #include "ops.h"
  39. #include "ov-scalar.h"
  40. #include "ov-re-mat.h"
  41. #include "pr-output.h"
  42.  
  43. octave_allocator
  44. octave_matrix::allocator (sizeof (octave_matrix));
  45.  
  46. int
  47. octave_matrix::t_id (-1);
  48.  
  49. const string
  50. octave_matrix::t_name ("matrix");
  51.  
  52. octave_matrix::octave_matrix (const RowVector& v, int pcv)
  53.   : octave_base_value (),
  54.     matrix ((pcv < 0 && Vprefer_column_vectors) || pcv
  55.         ? Matrix (v.transpose ()) : Matrix (v)) { }
  56.  
  57. octave_matrix::octave_matrix (const ColumnVector& v, int pcv)
  58.   : octave_base_value (),
  59.     matrix ((pcv < 0 && Vprefer_column_vectors) || pcv
  60.         ? Matrix (v) : Matrix (v.transpose ())) { }
  61.  
  62. #include <iostream.h>
  63.  
  64. octave_value *
  65. octave_matrix::try_narrowing_conversion (void)
  66. {
  67.   octave_value *retval = 0;
  68.  
  69.   int nr = matrix.rows ();
  70.   int nc = matrix.cols ();
  71.  
  72.   if (nr == 1 && nc == 1)
  73.     retval = new octave_scalar (matrix (0, 0));
  74.  
  75.   return retval;
  76. }
  77.  
  78. octave_value
  79. octave_matrix::index (const octave_value_list& idx) const
  80. {
  81.   octave_value retval;
  82.  
  83.   int len = idx.length ();
  84.  
  85.   switch (len)
  86.     {
  87.     case 2:
  88.       {
  89.     idx_vector i = idx (0).index_vector ();
  90.     idx_vector j = idx (1).index_vector ();
  91.  
  92.     retval = Matrix (matrix.index (i, j));
  93.       }
  94.       break;
  95.  
  96.     case 1:
  97.       {
  98.     idx_vector i = idx (0).index_vector ();
  99.  
  100.     retval = Matrix (matrix.index (i));
  101.       }
  102.       break;
  103.  
  104.     default:
  105.       error ("invalid number of indices (%d) for matrix value", len);
  106.       break;
  107.     }
  108.  
  109.   return retval;
  110. }
  111.  
  112. extern void assign (Array2<double>&, const Array2<double>&);
  113.  
  114. void
  115. octave_matrix::assign (const octave_value_list& idx, const Matrix& rhs)
  116. {
  117.   int len = idx.length ();
  118.  
  119.   switch (len)
  120.     {
  121.     case 2:
  122.       {
  123.     idx_vector i = idx (0).index_vector ();
  124.     idx_vector j = idx (1).index_vector ();
  125.  
  126.     matrix.set_index (i);
  127.     matrix.set_index (j);
  128.  
  129.     ::assign (matrix, rhs);
  130.       }
  131.       break;
  132.  
  133.     case 1:
  134.       {
  135.     idx_vector i = idx (0).index_vector ();
  136.  
  137.     matrix.set_index (i);
  138.  
  139.     ::assign (matrix, rhs);
  140.       }
  141.       break;
  142.  
  143.     default:
  144.       error ("invalid number of indices (%d) for indexed matrix assignment",
  145.          len);
  146.       break;
  147.     }
  148. }
  149.  
  150. bool
  151. octave_matrix::valid_as_scalar_index (void) const
  152. {
  153.   // XXX FIXME XXX
  154.   return false;
  155. }
  156.  
  157. bool
  158. octave_matrix::is_true (void) const
  159. {
  160.   bool retval = false;
  161.  
  162.   if (rows () == 0 || columns () == 0)
  163.     {
  164.       int flag = Vpropagate_empty_matrices;
  165.  
  166.       if (flag < 0)
  167.     warning ("empty matrix used in conditional expression");
  168.       else if (flag == 0)
  169.     error ("empty matrix used in conditional expression");
  170.     }
  171.   else
  172.     {
  173.       Matrix m = (matrix.all ()) . all ();
  174.  
  175.       retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0);
  176.     }
  177.  
  178.   return retval;
  179. }
  180.  
  181. double
  182. octave_matrix::double_value (bool) const
  183. {
  184.   double retval = octave_NaN;
  185.  
  186.   // XXX FIXME XXX -- maybe this should be a function, valid_as_scalar()
  187.   if ((rows () == 1 && columns () == 1)
  188.       || (Vdo_fortran_indexing && rows () > 0 && columns () > 0))
  189.     retval = matrix (0, 0);
  190.   else
  191.     gripe_invalid_conversion ("real matrix", "real scalar");
  192.  
  193.   return retval;
  194. }
  195.  
  196. Complex
  197. octave_matrix::complex_value (bool) const
  198. {
  199.   Complex retval (octave_NaN, octave_NaN);
  200.  
  201.   if ((rows () == 1 && columns () == 1)
  202.       || (Vdo_fortran_indexing && rows () > 0 && columns () > 0))
  203.     retval = matrix (0, 0);
  204.   else
  205.     gripe_invalid_conversion ("real matrix", "complex scalar");
  206.  
  207.   return retval;
  208. }
  209.  
  210. octave_value
  211. octave_matrix::convert_to_str (void) const
  212. {
  213.   octave_value retval;
  214.  
  215.   int nr = matrix.rows ();
  216.   int nc = matrix.columns ();
  217.  
  218.   if (nr == 0 && nc == 0)
  219.     {
  220.       char s = '\0';
  221.       retval = octave_value (&s);
  222.     }
  223.   else
  224.     {
  225.       if (nr == 0 || nc == 0)
  226.     {
  227.       char s = '\0';
  228.       retval = octave_value (&s);
  229.     }
  230.       else
  231.     {
  232.       charMatrix chm (nr, nc);
  233.  
  234.       for (int j = 0; j < nc; j++)
  235.         {
  236.           for (int i = 0; i < nr; i++)
  237.         {
  238.           double d = matrix (i, j);
  239.  
  240.           if (xisnan (d))
  241.             {
  242.               ::error ("invalid conversion from NaN to character");
  243.               return retval;
  244.             }
  245.           else
  246.             {
  247.               // XXX FIXME XXX -- warn about out of range
  248.               // conversions?
  249.  
  250.               int ival = NINT (d);
  251.               chm (i, j) = (char) ival;
  252.             }
  253.         }
  254.         }
  255.  
  256.       retval = octave_value (chm, 1);
  257.     }
  258.     }
  259.  
  260.   return retval;
  261. }
  262.  
  263. void
  264. octave_matrix::print (ostream& os, bool pr_as_read_syntax)
  265. {
  266.   octave_print_internal (os, matrix, pr_as_read_syntax, struct_indent);
  267. }
  268.  
  269. /*
  270. ;;; Local Variables: ***
  271. ;;; mode: C++ ***
  272. ;;; End: ***
  273. */
  274.